from PIL import Image
import os

def generate_app_icons(source_image_path):
    # 检查源文件是否存在
    if not os.path.exists(source_image_path):
        print(f"错误：找不到源图像文件 {source_image_path}")
        return
    
    # 定义需要生成的图标尺寸
    icon_sizes = {
        "android": [72, 96, 144, 192],
        "ios": [1024]
    }
    
    try:
        # 打开源图像
        with Image.open(source_image_path) as img:
            print(f"成功打开源图像：{source_image_path}")
            
            # 生成Android图标
            for size in icon_sizes["android"]:
                # 调整图像大小，保持宽高比
                resized_img = img.resize((size, size), Image.LANCZOS)
                # 保存调整后的图像
                output_path = f"android-icon-{size}x{size}.png"
                resized_img.save(output_path, format="PNG")
                print(f"已生成Android图标：{output_path}")
            
            # 生成iOS图标
            for size in icon_sizes["ios"]:
                # 调整图像大小，保持宽高比
                resized_img = img.resize((size, size), Image.LANCZOS)
                # 保存调整后的图像
                output_path = f"ios-icon-{size}x{size}.png"
                resized_img.save(output_path, format="PNG")
                print(f"已生成iOS图标：{output_path}")
        
        print("\n所有图标生成完成！")
    
    except Exception as e:
        print(f"生成图标时出错：{e}")

if __name__ == "__main__":
    # 使用当前目录下的logo.jpg作为源图像
    source_image = "logo.jpg"
    generate_app_icons(source_image)